package com.demo.studentidcard.controller;

import com.demo.studentidcard.entity.Account;
import com.demo.studentidcard.service.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author 8
 * @date 2021/9/23 23:46
 */
@Slf4j
@Controller
public class ThymeleafController {

    private final BaseService baseService;

    @Autowired
    public ThymeleafController(BaseService baseService) {
        this.baseService = baseService;
    }


    @PostMapping("/resignService")
    public String resignService(Account account) {
        return baseService.resign(account) ? "success" : "fail";
    }

    @PostMapping("/rechargeService")
    public String rechargeService(String id, String strMoney){
        return baseService.recharge(id, Integer.parseInt(strMoney)) ? "success" : "fail";
    }

    @PostMapping("/getBalanceService")
    public String getBalanceService(String id, Model model){
        int balance = baseService.getBalance(id);
        model.addAttribute("msg", String.valueOf(balance));
        return "balance";
    }

    @PostMapping("/isLostService")
    public String isLostService(String id){
        return baseService.setIsLost(id) ? "success" : "fail";
    }

    @RequestMapping("/setIsLostNoneService")
    public String setIsLostNoneService(String id){
        return baseService.setIsLostNone(id) ? "success" : "fail";
    }

    @PostMapping("/showDetailsService")
    public String showDetailsService(String id, Model model){
        model.addAttribute("msg", baseService.getRecordsOfConsumption(id));
        return "details";
    }

}
